package com.dianrui.operation.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dianrui.operation.Constant;
import com.dianrui.operation.activity.BaseActivity;
import com.dianrui.operation.util.SPUtils;
import com.dianrui.operation.util.Static;

import butterknife.ButterKnife;

public abstract class BaseFragment extends Fragment {
    protected LayoutInflater mInflater;
    protected View rootView;
    public FragmentManager FRG_MANAGER;
    public SPUtils spUtils;
    public String getTAG() {
        return getClass().getSimpleName();
    }
    protected Activity mContext;


    @Override
    public void onAttach(Activity activity) {
        if (activity instanceof BaseActivity) {
            mContext = activity;
        }
        super.onAttach(activity);
    }

    @SuppressLint("RestrictedApi")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.e("fragment name:",getClass().getName());
        super.onCreate(savedInstanceState);
        FRG_MANAGER = getActivity().getSupportFragmentManager();
        this.mInflater = this.getLayoutInflater(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(getContentViewId(),container,false);
        ButterKnife.bind(this, rootView);//绑定framgent
        spUtils = new SPUtils(getActivity(), Constant.cfgFileName, Context.MODE_PRIVATE);
        initView();

        return rootView;
    }

    public Resources getSysResources() {//所有Fragment里面获取Resource都用这个
        if (isAdded()) {
            return getResources();
        } else {
            return Static.CONTEXT.getResources();
        }
    }

    public abstract int getContentViewId();//布局ID

    public abstract void initView();


    protected void JumpActivitys(Class<?> clazz) {
        Intent intent = new Intent(getActivity(), clazz);
        startActivity(intent);
    }

    protected void JumpKillActivitys(Class<?> clazz) {
        Intent intent = new Intent(getActivity(), clazz);
        startActivity(intent);
        getActivity().finish();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
